package com.zt.bus.assistant.service;

import com.zt.bus.assistant.inter.HolidayMapper;
import com.zt.bus.assistant.model.Holiday;
import com.zt.bus.constant.BusConstant;
import com.zt.servlet.ReqBean;
import com.zt.system.model.BusinessException;
import com.zt.system.service.BaseService;
import com.zt.util.DateUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 工作日、节假日 Created by Administrator on 2017/6/1.
 */
@Service
public class HolidayService extends BaseService {

    @Autowired
    private HolidayMapper holidayMapper;
    @Autowired
    private CacheManager cacheManager;

    /**
     * 判断该日期是否是节假日
     * 
     * @param date
     * @return
     */
    public boolean isHoliday(Date date) {
        boolean flag = false;
        if (date == null) {
            throw new BusinessException("日期不能为空");
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);

        List<Holiday> holidayList = getHolidayFromYear(year);
        for (int i = 0; i < holidayList.size(); i++) {
            Holiday holiday = holidayList.get(i);
            if (DateUtil.toString(date).equals(DateUtil.toString(holiday.getHolidayDate()))) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * 查询某一年内所有的节假日
     * 
     * @param year
     * @return
     */
    public List<Holiday> getHolidayFromYear(int year) {
        // 缓存
        List<Holiday> list = null;
        Cache cache = cacheManager.getCache(BusConstant.Cache.HOLIDAY);
        Object value = cache.get("holiday_year_" + year);
        if (value instanceof SimpleValueWrapper) {// 存在缓存
            list = (List<Holiday>) ((SimpleValueWrapper) value).get();
        } else {
            list = holidayMapper.selectByYear(year);
            cache.put("holiday_year_" + year, list);
        }
        return list;
    }

    /**
     * 查询
     * 
     * @param reqBean
     * @return
     */
    public JSONArray getTable(ReqBean reqBean) {
        Date start = reqBean.getInfoDate("startDate");
        Date end = reqBean.getInfoDate("endDate");
        StringBuffer sql = new StringBuffer(
                "select * from DZ_HOLIDAY t where HOLIDAY_DATE >= ? and HOLIDAY_DATE <= ? order by HOLIDAY_DATE");
        return this.query(sql.toString(), start, end);
    }

    @CacheEvict(value = BusConstant.Cache.HOLIDAY, allEntries = true)
    public void save(int year, List<Date> list) throws Exception {
        holidayMapper.deleteByYear(year);
        for (Date date : list) {
            Holiday holiday = new Holiday();
            holiday.setHolidayName("");
            holiday.setHolidayDate(date);
            holiday.setYear(year);
            holiday.setColor("gray");// 节假日背景色
            holidayMapper.insert(holiday);
        }
    }
}
